import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';
import legacy from '@vitejs/plugin-legacy';
import { Env } from '@/vite_env';
import viteCompression from 'vite-plugin-compression';
import removeConsole from 'vite-plugin-remove-console';
import { createHtmlPlugin } from 'vite-plugin-html';
import { visualizer } from 'rollup-plugin-visualizer';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import browserslist from 'browserslist';
const browserslistConfig = browserslist.loadConfig({ path: '.browserslistrc' });
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd(), '') as unknown as Env;
  return {
    base: env.VITE_PUBLIC_PATH,
    plugins: [
      react(),
      command === 'build'
        ? legacy({
            targets: [
              ...browserslistConfig!,
              'defaults',
              'ie >= 11',
              'chrome 52',
            ],
            additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
            renderLegacyChunks: true,
          })
        : null, //打包后对浏览器兼容
      viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        algorithm: 'gzip',
      }), //打包后压缩
      command === 'build' ? removeConsole() : null, //生成环境移除console
      createHtmlPlugin({
        minify: true,
        template: 'index.html',
        inject: {
          data: {
            title: env.VITE_TITLE,
          },
        },
      }), //html模板引擎
      visualizer({
        gzipSize: true,
        brotliSize: true,
        emitFile: false,
        filename: 'stats.html',
        open: false,
      }), //打包后分析打包文件大小
      createSvgIconsPlugin({
        iconDirs: [resolve(process.cwd(), 'src/assets/svgs')],
        symbolId: 'icon-[dir]-[name]',
      }), //svg图标
    ].filter(Boolean),
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
    },
    server: {
      host: '0.0.0.0',
      proxy: {
        '/custom/runtime': {
          target: 'http://127.0.0.1:5500',
          changeOrigin: true,
          rewrite: (path: string) =>
            path.replace(/^\/custom\/runtime/, '/packages/jssdk/dist'),
        },
        '/api/custom/v1/plugin/PcWeb': {
          target: 'http://127.0.0.1:5500',
          changeOrigin: true,
          rewrite: (path) =>
            path.replace(
              /^\/api\/custom\/v1\/plugin\/PcWeb/,
              '/demos/PcWeb.json'
            ),
        },
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true, // 支持内联 JavaScript
          modifyVars: {
            // 更改主题
          },
        },
      },
    },
    build: {
      outDir: 'dist',
      assetsDir: 'assets',
      sourcemap: false,
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return 'vendor';
            }
          },
        },
      },
    },
  };
});
